﻿using System;
using EnterpriseLibraryExtensions.Events.Aggregation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Events.PolicyInjection
{
	[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
	public sealed class EventAggregationCallHandlerAttribute : HandlerAttribute
	{
		public EventAggregationCallHandlerAttribute()
			: this(typeof(NoFilteringOptions), EventAggregationMode.Required)
		{
		}

		public EventAggregationCallHandlerAttribute(EventAggregationMode eventAggregationMode)
			: this(typeof(NoFilteringOptions), eventAggregationMode)
		{
		}

		public EventAggregationCallHandlerAttribute(Type scopeOptionsType)
			: this(scopeOptionsType, EventAggregationMode.Required)
		{
		}

		public EventAggregationCallHandlerAttribute(Type scopeOptionsType, EventAggregationMode eventAggregationMode)
		{
			if (scopeOptionsType == null)
			{
				throw new ArgumentNullException("scopeOptionsType");
			}

			ScopeOptionsType = scopeOptionsType;
			EventAggregationMode = eventAggregationMode;
		}

		public Type ScopeOptionsType { get; private set; }

		public EventAggregationMode EventAggregationMode { get; private set; }

		public override ICallHandler CreateHandler(IUnityContainer container)
		{
			return new EventAggregationCallHandler(ScopeOptionsType, EventAggregationMode);
		}
	}
}
